package com.course.server.service;

import cn.hutool.core.util.StrUtil;
import com.course.server.dto.CourseContentDto;
import com.course.server.dto.CourseDto;
import com.course.server.dto.PageDto;
import com.course.server.entity.Course;
import com.course.server.entity.CourseContent;
import com.course.server.entity.CourseExample;
import com.course.server.mapper.CourseContentMapper;
import com.course.server.mapper.CourseMapper;
import com.course.server.mapper.MCourseMapper;
import com.course.server.util.CopyUtil;
import com.course.server.util.UuidUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: Durian
 * @date: 2021/5/2 11:02
 */
@Service
public class CourseService {

    @Resource
    private CourseMapper courseMapper;
    @Resource
    private MCourseMapper mCourseMapper;
    @Resource
    private CourseCategoryService courseCategoryService;
    @Resource
    private CourseContentMapper courseContentMapper;

    public PageDto<CourseDto> list(int page, int size, String courseId) {
        PageHelper.startPage(page, size);
        CourseExample example = new CourseExample();
        CourseExample.Criteria criteria = example.createCriteria();
        if (StrUtil.isNotBlank(courseId)) {
            criteria.andIdEqualTo(courseId);
        }
        example.setOrderByClause("sort asc");
        List<Course> courses = courseMapper.selectByExample(example);
        PageInfo<Course> pageInfo = new PageInfo<>(courses);

        List<CourseDto> courseDtos = CopyUtil.copyList(courses, CourseDto.class);
        PageDto<CourseDto> pageDto = new PageDto<>();
        pageDto.setPage(page);
        pageDto.setSize(size);
        pageDto.setList(courseDtos);
        pageDto.setTotal(pageInfo.getTotal());
        return pageDto;
    }

    @Transactional(rollbackFor = {Exception.class})
    public void save(CourseDto courseDto) {
        Course course = new Course();
        BeanUtils.copyProperties(courseDto, course);
        if (StrUtil.isBlank(courseDto.getId())) {
            insert(course);
        } else {
            update(course);
        }
        // 保存课程分类
        courseCategoryService.batchSave(course.getId(), courseDto.getCategories());
    }

    public void delete(String id) {
        courseMapper.deleteByPrimaryKey(id);
    }

    private void insert(Course course) {
        course.setId(UuidUtils.getShortUuid());
        course.setCreatedAt(LocalDateTime.now());
        course.setUpdatedAt(LocalDateTime.now());
        course.setEnroll(0);
        courseMapper.insert(course);
    }

    private void update(Course course) {
        course.setUpdatedAt(LocalDateTime.now());
        courseMapper.updateByPrimaryKeySelective(course);
    }

    public void updateTime(String courseId) {
        mCourseMapper.updateTime(courseId);
    }

    public CourseContentDto findCourseContent(String id) {
        return CopyUtil.copy(courseContentMapper.selectByPrimaryKey(id), CourseContentDto.class);
    }

    public void saveCourseContent(CourseContentDto courseContentDto) {
        CourseContent courseContent = CopyUtil.copy(courseContentDto, CourseContent.class);
        // 直接更新，减少查找次数
        if (courseContentMapper.updateByPrimaryKeyWithBLOBs(courseContent) == 0) {
            courseContentMapper.insert(courseContent);
        }
    }

}
